<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	
	public function index()
	{
		$this->load->helper('url');
		$this->load->model('admin_nav_model');
		$menu = $this->admin_nav_model->get_nav('admin');
		
		// auth
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}
		
		$config['view'] = 'admin/admin_home_view';
		$config['menu'] = $menu;
		
		$config['user'] = $this->quickauth->user()->username;
		
		$this->load->view('global_view', $config);		
	}
	
	public function post_type($id = NULL)
	{
		$this->load->helper('url');
		// auth
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}
		
		$this->load->library(array('form_validation', 'session'));
		
		$this->load->model('admin_nav_model');
		if($id != NULL)
			$menu = $this->admin_nav_model->get_nav();	
		else
			$menu = $this->admin_nav_model->get_nav('admin/post_type');
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post_type');
		$post_type = $this->db_model->findAll(NULL, NULL, 'order');
		
		$name = '';
		$order = '';
		if($id != NULL)
		{
			$name = $this->db_model->read($id)->name;
			$order = $this->db_model->read($id)->order;	
		}
//		$this->form_validation->set_message('required', 'hello world');
//		$this->form_validation->set_message('username_check');
		$this->form_validation->set_rules('name', '分类名称', 'required');		
		$this->form_validation->set_rules('order', 'order', 'required');
//		$this->form_validation->set_rules('url', 'url', 'required|prep_url');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		
		$config['view'] = 'admin/admin_post_type_view';
		$config['menu'] = $menu;
		$config['post_type'] = $post_type;
		$config['name'] = $name;
		$config['order'] = $order;
		$config['id'] = $id;
		if($id != NULL)
		{
			$config['post_type_title'] = '编辑分类';
		}
		else
		{
			$config['post_type_title'] = '增加分类';
		}
		
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('global_view', $config);
		}
		else
		{			
			
			if($this->input->post('id') == NULL)
				$this->post_type_add();
			else
				$this->post_type_edit($this->input->post('id'));	
		}	
		
			
	}
	
	function post_type_add()
	{
		$this->load->model('db_model');		
		$this->db_model->loadTable('post_type');
		$this->db_model->save($_POST);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '增加成功');
		
		$this->load->helper('url');
		redirect('admin/post_type', 'refresh');		
	}
	
	function post_type_edit($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		$this->load->model('db_model');		
		$this->db_model->loadTable('post_type');
		$this->db_model->save($_POST, $id);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '编辑成功');
		
		$this->load->helper('url');
		redirect('admin/post_type/' . $id, 'refresh');		
	}
	
	function post_type_delete($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post_type');
		$this->db_model->remove($id);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '删除成功');
		
		$this->load->helper('url');
		redirect('admin/post_type', 'refresh');
	}
	
	public function post($id = NULL)
	{
		$this->load->helper('url');
		// auth
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}
		
		$this->load->library(array('form_validation', 'session'));
		
		$this->load->model('admin_nav_model');
		if($id != NULL)
			$menu = $this->admin_nav_model->get_nav();
		else
			$menu = $this->admin_nav_model->get_nav('admin/post');
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post_type');
		$post_type = $this->db_model->findAll(NULL, NULL, 'order');
	
		$post = new stdClass();
		$post->name = '';
		$post->content = '';
		$post->typeid = '';
		$post->order = '';
		$post->keywords = '';
		if($id != NULL)
		{
			$this->db_model->loadTable('post');
			$post = $this->db_model->read($id);	
		}
		
		
		
		$this->form_validation->set_rules('name', '文章名称', 'required');		
		$this->form_validation->set_rules('content', '文章内容', 'required');
		$this->form_validation->set_rules('typeid', 'typeid', 'required');
		$this->form_validation->set_rules('order', 'order', 'required');
		$this->form_validation->set_rules('keywords');
//		$this->form_validation->set_rules('url', 'url', 'required|prep_url');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		
		$config['view'] = 'admin/admin_post_view';
		$config['menu'] = $menu;
		$config['post_type'] = $post_type;
		$config['post'] = $post;
		$config['id'] = $id;
		if($id != NULL)
		{
			$config['post_title'] = '编辑文章';
		}
		else
		{
			$config['post_title'] = '增加文章';
		}
		
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('global_view', $config);
		}
		else
		{			
			
			if($this->input->post('id') == NULL)
				$this->post_add();
			else
				$this->post_edit($this->input->post('id'));	
		}	
		
			
	}
	
	function post_add()
	{
		$insert_array = array(
			'name'=>$this->input->post('name'),
			'content'=>$this->input->post('content'),
			'typeid'=>$this->input->post('typeid'),
			'order'=>$this->input->post('order'),
			'keywords'=>$this->input->post('keywords'),
			'create'=>date('Y-m-d H:i:s'),
			'update'=>date('Y-m-d H:i:s'),
		);
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post');		
		$this->db_model->save($insert_array);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '增加成功');
		
		$this->load->helper('url');
		redirect('admin/post', 'refresh');		
	}
	
	function post_edit($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$edit_array = array(
			'name'=>$this->input->post('name'),
			'content'=>$this->input->post('content'),
			'typeid'=>$this->input->post('typeid'),
			'order'=>$this->input->post('order'),
			'keywords'=>$this->input->post('keywords'),
			'update'=>date('Y-m-d H:i:s'),
		);
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post');
		$this->db_model->save($edit_array, $id);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '编辑成功');
		
		$this->load->helper('url');
		redirect('admin/post/' . $id, 'refresh');
		//$this->post($id);		
	}
	
	function post_delete($id = NULL)
	{
		if($id == NULL)
		{
			return FALSE;
		}
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post');
		$this->db_model->remove($id);
		
		$this->load->library('session');
		$this->session->set_flashdata('message', '删除成功');
		
		$this->load->helper('url');
		redirect('admin/post_list', 'refresh');
	}
	
	public function post_list()
	{
		$this->load->helper('url');
		// auth
		$this->load->library('quickauth');
		if(!$this->quickauth->logged_in())
		{
			redirect('user','refresh');
		}
		
		
		$this->load->library(array('form_validation'));
		
		$this->load->model('admin_nav_model');
		$menu = $this->admin_nav_model->get_nav('admin/post_list');
		
		$this->load->model('db_model');		
		$this->db_model->loadTable('post');
		$post = $this->db_model->findAll(NULL, array('id', 'name'));
		
		
		
		$config['view'] = 'admin/admin_post_list_view';
		$config['menu'] = $menu;
		$config['post'] = $post;
		
		$this->load->view('global_view', $config);	
		
			
	}
	
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */